<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="药品名称" prop="drugName">
      <el-input v-model="dataForm.drugName" placeholder="药品名称"></el-input>
    </el-form-item>
      <el-form-item label="药品类别" prop="drugType">
        <el-select v-model="dataForm.drugType" placeholder="请选择">
          <el-option label="中药" value="中药"></el-option>
          <el-option label="西药" value="西药"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="试剂类型" prop="drugReagtype">
        <el-select v-model="dataForm.drugReagtype" placeholder="请选择">
          <el-option label="口服" value="口服"></el-option>
          <el-option label="冲泡" value="冲泡"></el-option>
          <el-option label="外用" value="外用"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="药品简码" prop="drugCode">
      <el-input v-model="dataForm.drugCode" placeholder="药品简码"></el-input>
    </el-form-item>
    <el-form-item label="药品规格" prop="drugLevel">
      <el-input v-model="dataForm.drugLevel" placeholder="药品规格"></el-input>
    </el-form-item>
    <el-form-item label="生产厂家" prop="drugAddress">
      <el-input v-model="dataForm.drugAddress" placeholder="生产厂家"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          drugName: '',
          drugType: '',
          drugReagtype: '',
          drugCode: '',
          drugLevel: '',
          drugAddress: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          drugName: [
            { required: true, message: '药品名称不能为空', trigger: 'blur' }
          ],
          drugType: [
            { required: true, message: '药品类别 Z-中药 X-西药不能为空', trigger: 'blur' }
          ],
          drugReagtype: [
            { required: true, message: '试剂类型不能为空', trigger: 'blur' }
          ],
          drugCode: [
            { required: true, message: '药品简码不能为空', trigger: 'blur' }
          ],
          drugLevel: [
            { required: true, message: '药品规格不能为空', trigger: 'blur' }
          ],
          drugAddress: [
            { required: true, message: '生产厂家不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_drug/bsdrug/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.drugName = data.bsDrug.drugName
                this.dataForm.drugType = data.bsDrug.drugType
                this.dataForm.drugReagtype = data.bsDrug.drugReagtype
                this.dataForm.drugCode = data.bsDrug.drugCode
                this.dataForm.drugLevel = data.bsDrug.drugLevel
                this.dataForm.drugAddress = data.bsDrug.drugAddress
                this.dataForm.createTime = data.bsDrug.createTime
                this.dataForm.updateTime = data.bsDrug.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_drug/bsdrug/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'drugName': this.dataForm.drugName,
                'drugType': this.dataForm.drugType,
                'drugReagtype': this.dataForm.drugReagtype,
                'drugCode': this.dataForm.drugCode,
                'drugLevel': this.dataForm.drugLevel,
                'drugAddress': this.dataForm.drugAddress,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
